<template>
	<view class="root">
		<view v-if="loaded" class="blog-container">
			<text class="blog-title">{{blog.title}}</text>
			<text class="item-username">{{blog.user.nickName}}{{blog.createTime}}</text>
			<!-- <text class="item-desc">{{item.description}}</text> -->
			<rich-text :nodes="blog.content" class="blog-container"></rich-text>
			<image :src="blog.picture" mode="widthFix" class="img-focus"></image>
			<view>
				<text v-for="(tag,idx) in blog.tags" class="item-tag" :key="idx">{{tag}}</text>
			</view>
			<view class="icons-container">
				<text @click.stop="changeGood(blog.id)"
					:class="good_ids.indexOf(blog.id)>=0?'iconfont icon-dianzan3 icon-good1':'iconfont icon-dianzan3 icon-goods '"></text>
				<text class="icon-text">{{blog.good}}</text>
				<!-- <image src="../../static/icons/commit.png" class="icon"></image> -->
				<text class="icon iconfont icon-ic_daipingji2"></text>
				<text class="icon-text">{{blog.comments.length}}</text>
				<!-- <image src="../../static/icons/ic_count.png" class="icon"></image> -->
				<text class="icon iconfont icon-a-ziyuan563ldpi"></text>
				<text class="icon-text">{{blog.readCount}}</text>
			</view>
			<image :src="blog.user.avatar" mode="aspectFill" class="img-avatar"></image>
		</view>
		<view v-if="hasComments" class="blog-margin-continer">
			<view v-for="(item,index) in parents" :key="item.id">
				<view class="cmt-container">
					<text class="cmt-nick">{{item.nickName}}</text>
					<view class="cmt-time-container">
						<text class="cmt-time">{{item.createTime}}</text>
						<text class="txt-delete">[删除]</text>
					</view>
					<image :src="item.avatar" class="cmt-avatar" mode="aspectFill"></image>
					<text class="cmt-content" @click="clickReply(item)">{{item.content}}</text>
				</view>
				<view v-if="replies[index].length > 0" class="replies-container">
					<view v-for="(reply,idx) in replies[index]" class="reply-item" :key="reply.id">
						<view class="reoly-info">
							<text class="reply-nick">{{reply.nickName}}</text>
							<text class="reply-to">@{{reply.toUser}}</text>
							<text class="reply-time">{{reply.createTime}}</text>
						</view>
						<text class="reply-content">{{reply.content}}</text>
					</view>
				</view>
				<view v-if="index < parents.length - 1" class="cmt-line"></view>
			</view>
			<view class="cmt-linee"></view>
		</view>
		<view class="input-container">
			<image :src="userAvatar" mode="aspectFill" class="input-avatar" @click="clickLogin"></image>
			<textarea @input="inputGetContent" auto-height :placeholder="inputHolder" class="input-area" placeholder-class="input-holder"/>
			<text class="iconfont icon-xiangqing icon-xiangqing" @click="clickCommit"></text>
		</view>
		<login-dailog ref="login"@after="afterLogin"></login-dailog>
	</view>
</template>

<script>
	let parentId = null
	export default {
		data() {
			return {
				blog: {},
				good_ids: [],
				loaded: false,
				parents: [],
				replies: [],
				hasComments: false,
				inputHolder: "请输入评论内容......",
				inputContent: "",
				userAvatar:"../../static/icons/default_user.png"
			}
		},
		onLoad(options) {
			this.$noti.register(this.$params.noti_login_status,this.afterLogin,this)
			let ids = uni.getStorageSync(this.$params.key_good_ids)
			if (ids) {
				this.good_ids = ids
			}
			let id = options.id
			// this.getBlog(id)
			this.addReadCount(id)
		},
		onUnload(){
			this.$noti.remove(this.$params.noti_login_status, this)
		},
		methods: {
			afterLogin(){
				this.userAvatar = this.$params.host + getApp().globalData.avatar
			},
			clickLogin(){
				this.$refs.login.show()
			},
			getBlog(id) {
				let url = this.$params.host + this.$params.action_top_blogs + id
				uni.showLoading({
					title: '加载中..',
					mask: false
				});
				this.$req.get(url, res => {
					// console.log(res)
					let item = res.data.data
					uni.setNavigationBarTitle({
						title: item.user.nickName
					})
					item.picture = this.$params.host + item.picture
					item.user.avatar = this.$params.host + item.user.avatar
					item.content = item.content.replace(/\<img/gi, '<img class="rich-img"')
					item.createTime = this.$util.formatTime(new Date(item.createTime))
					item.updateTime = this.$util.formatTime(new Date(item.updateTime))
					this.formatComments(item.comments)
					this.blog = item
					this.loaded = true
					// console.log(this.blog.content);
				}, () => {
					uni.hideLoading()
				})
			},
			formatComments(comments) {
				if (comments.length == 0) {
					return
				}
				let parents = []
				comments.forEach(c => {
					c.avatar = this.$params.host + c.avatar.substring(1)
					c.createTime = this.$util.formatTime(new Date(c.createTime))
					if (c.parentId == null) {
						parents.push(c)
					}
				})
				parents.sort((c1, c2) => {
					return new Date(c2.createTime).getTime() - new Date(c1.createTime).getTime()
				})
				let replies = new Array(parents.length).fill([])
				let index = 0
				parents.forEach(p => {
					let cmmts = []
					cmmts = [...cmmts, ...this.getAllChildren(p, comments)]
					replies[index++] = cmmts
				})
				this.parents = parents
				this.replies = replies
				this.hasComments = parents.length > 0
			},
			getAllChildren(parent, comments) {
				let result = []
				comments.forEach(c => {
					if (c.parentId == parent.id) {
						c.toUser = parent.nickName
						result.push(c)
						result = [...result, ...this.getAllChildren(c, comments)]
					}
				})
				return result
			},
			addReadCount(id) {
				let url = this.$params.host + this.$params.action_read_count
				let data = {
					"id": id
				}
				this.$req.postQuery(url, data, res => {
					// console.log(res);

					let count = res.data.data
					this.$noti.post(this.$params.noti_refresh_count, {
						id,
						count
					})
					// let pages = getCurrentPages()
					// let prev = pages[pages.length-2]
					// prev.refreshReadCount(id,count)
				}, () => {
					this.getBlog(id)
				})
			},

			changeGood(id) {
				let good = true
				if (this.good_ids.indexOf(id) >= 0) {
					good = false
				}
				if (good) {
					this.good_ids = [...this.good_ids, id]
				} else {
					//splice删除指定位置元素
					//shift删除第一个，pop删除最后一个
					let pos = this.good_ids.indexOf(id)
					this.good_ids.splice(pos, 1)
				}
				uni.setStorageSync(this.$params.key_good_ids, this.good_ids)
				//发生数据到服务器，修改点赞数量+1或-1，服务器返回最新点赞数量
				this.postGood(id, good)
			},

			postGood(id, good) {
				uni.showLoading({
					title: "点赞",
					mask: false
				});
				let url = this.$params.host
				let action = good ? this.$params.action_good : this.$params.action_del_good
				url += action
				let data = {
					"id": id
				}
				this.$req.postQuery(url, data, res => {
					console.log(res);

					this.blogs.forEach(blog => {
						if (blog.id == id) {
							blog.good = res.data.data;
						}
					});

				}, () => {
					uni.hideLoading()
				})
			},
			clickReply(reply) {
				parentId = reply.id
				this.inputHolder = "@"+reply.nickName
			},
			inputGetContent(e){
				this.inputContent = e.detail.value
			},
			clickCommit(){
				console.log(parentId, this.inputContent);
			}
		}
	}
</script>

<style>
@import url("detail.css");
</style>
